<?php

App::uses('AppController', 'Controller');

class EventsController extends AppController {
	public $name = 'Events';
	public $uses = array('EventsType', 'EventsTypeFields', 'EventsCategories', 'Events', 'Tournaments', 'TournamentDates', 'TurnamentMatchs', 'EventsDBFields', 'EventFields');
	public $helpers = array('Session');
	public $components = array('Paginator');

	public function eventstypeList()
	{
		$eventsTypes = $this->paginate('EventsType');
		$this->set('page_title',"Tipos de Eventos");
		$this->set('eventsType',$eventsTypes);	
	}
	
	public function eventstype_add($eventtype_id = null)
	{
		$data = $this->data;
		if(!empty($data)){
			$this->EventsType->set($data);
			if($this->EventsType->validates()){
				$data['EventsType'] = $data['Events'];
				
				if($this->EventsType->save($data))
				{
					$this->Session->setFlash(__("Se registro exitosamente el tipo de evento ".$this->data['EventsType']['name'], true), 'flash', array('class' => 'muted'));
					$this->redirect(array('action' => 'eventstypeList'));
				}
				else
				{		
					$errors = $this->EventsType->validationErrors;
					$this->Session->setFlash(__("Error", true), 'flash_error', array('class' => 'alert alert-error'));
				}
			}
			else{
			$errors = $this->EventsType->validationErrors;
			//$this->Session->setFlash(__($errors, true), 'flash_error', array('class' => 'alert alert-error'));
			$this->Session->setFlash(__("Error.", true), 'flash_error', array('class' => 'alert alert-error'));
			}
		}
		
		//if($eventtype_id)
		//{
		//	$data = $this->EventsType->findByid($eventtype_id);
		//}
		
		
		$this->set('categories', $this->EventsCategories->findAllBystatus('AC'));
		$this->set('page_title',$eventtype_id == null ? "Nuevo Tipo de Evento" : "Editar Tipo de Evento");	
	}
	
	public function eventstype_edit($eventtype_id)
	{
		
	}
	
	public function eventstype_edit_fields($eventtype_id){
		
		$data = $this->EventsType->findByid($eventtype_id);
		$this->set('eventstype',$data);	
	}
	
	public function eventstype_save_fields()
	{		
		$data = $this->data;		
		if(!empty($data)){
			$this->EventsTypeFields->set($data);
			if($this->EventsTypeFields->validates()){
				$data['EventsTypeFields'] = $data['Events'];
				
				if($this->EventsTypeFields->save($data))
				{

					$this->Session->setFlash(__("Se registro exitosamente el tipo de evento ".$this->data['EventsType']['name'], true), 'flash', array('class' => 'muted'));
					return $this->redirect(array('action' => 'eventstype_edit_fields', 7));
				}
				else{		
				$errors = $this->EventsTypeFields->validationErrors;
				var_dump($errors);
				$this->Session->setFlash(__("Error", true), 'flash_error', array('class' => 'alert alert-error'));
				}
			}
			else{
			$errors = $this->EventsTypeFields->validationErrors;
			//$this->Session->setFlash(__($errors, true), 'flash_error', array('class' => 'alert alert-error'));
			$this->Session->setFlash(__("Error.", true), 'flash_error', array('class' => 'alert alert-error'));
			}
			
		}
		
		return $this->redirect(array('action' => 'eventstype_edit_fields', 7));
	}
	
	public function event_list()
	{
		$types = $this->EventsType->findAllBystatus('AC');
		$this->set('title', "Eventos");
		$this->set('types',$types);
		
	}
	
	public function events_list_and_repositorie($eventTypeId, $categorieId)
	{
		$data['actives'] = $this->Events->find('all', array(
					'conditions' => array('status' => 'AC', 'cateventstype_id' => $eventTypeId)
				    ));
		$data['eventTypeId'] = $eventTypeId;
		$info = $this->EventsType->find('first', array(
					'fields'     => array('EventsType.catcategories_code'),
					'conditions' => array('id' => $categorieId)
				    ));
		 
		$data['categorieCode'] = $info['EventsType']['catcategories_code'];
		$this->set('data', $data);
		$this->layout = false;
	}
	
	public function get_events_by_type($eventTypeId, $status)
	{
		$events['events'] = $this->Events->find('all', array(
					'conditions' => array('status' => $status, 'cateventstype_id' => $eventTypeId)
				    ));
		$events['status'] = $status;
		$this->set('events_data', $events);
		$this->layout = false;
	}
	
	public function get_futbol_data_repositorie()
	{
		$data = $this->Tournaments->findAllBystatus('AC');
		$this->set('data', $data);
		$this->layout = false;
	}
	
	public function get_futbol_data_repositorie_dates($torunamentId)
	{
		$data = $this->TournamentDates->findAllBycattournament_id($torunamentId);
		$this->set('data', $data);
		$this->layout = false;
	}
	
	public function get_futbol_data_repositorie_matchs($dateId)
	{
		$data = $this->TurnamentMatchs->findAllBycattournamentdate_id($dateId);
		$this->set('data', $data);
		$this->layout = false;
	}
	
	public function get_futbol_data_repositorie_dinamic_fields($eventTypeId, $MatchId)
	{
		$data['fields'] = $this->EventsTypeFields->findAllBycateventstype_id($eventTypeId);
		$data['match'] = $this->TurnamentMatchs->findBymatchid($MatchId);
		$data['fieldDB'] = $this->EventsDBFields->findAllBycateventstypes_id($eventTypeId);
		$this->set('data', $data);
		$this->layout = false;
	}
	
	public function set_futbol_event($matchId, $eventTypeId, $nameEvent)
	{
		$data['Events']['name'] = $nameEvent;
		$data['Events']['cateventstype_id'] = $eventTypeId;
		$data['Events']['identificator'] = "$matchId";
		$data['Events']['status'] = "AC";
		$data['Events']['sisusers_id'] = 1;
		$this->Events->save($data);
		
		$eventId = $this->Events->find('first', array('fields' => array('MAX(Events.id) as max_id')));
		
		
		$fields = json_decode($_POST['_json']);
		$fields = str_replace("[", "", $fields);
		$fields = str_replace("]", "", $fields);
		$fields = split(",",$fields);
		
		foreach($fields as $f)
		{
			$f = str_replace("}", "", $f);
			$f = str_replace("{", "", $f);
			$f = str_replace("\"", "", $f);
			$f = split(":",$f);
			
			$dataD['EventFields']['catevents_id'] = $eventId[0]['max_id'];
			$dataD['EventFields']['cateventfields_id'] = $f[0];
			$dataD['EventFields']['value'] = $f[1];
			$this->EventFields->save($dataD);
		}
		
		$this->set('data', $nameEvent);
		$this->layout = false;
	}
}
